package com.gaoxk.utils;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * 金额计算工具类
 *
 * @author yuangz
 */
public class MoneyUtil {
    /**
     * 乘法运算
     *
     * @param unitPrice     乘数
     * @param share         被乘数
     * @param decimalDigits 返回字符串保留小数位数 负数不设置
     * @return
     */
    public static String getTotal(String unitPrice, String share, int decimalDigits) throws FailException {
        try {
            if (unitPrice == null || "".equals(unitPrice))
                return "";
            BigDecimal totalNum = stringToNumber(unitPrice).multiply(stringToNumber(share));
            return getNumberFormat(totalNum, decimalDigits);
        } catch (Exception e) {
            throw new FailException("乘法运算出错 " + unitPrice + "*" + share);
        }
    }

    /**
     * 乘法运算[向下取整]
     *
     * @param unitPrice     乘数
     * @param share         被乘数
     * @param decimalDigits 返回字符串保留小数位数 负数不设置
     * @return
     */
    public static String getRoundDownTotal(String unitPrice, String share, int decimalDigits) throws FailException {
        try {
            BigDecimal totalNum = stringToNumber(unitPrice).multiply(stringToNumber(share));
            return getRoundDownNumberFormat(totalNum, decimalDigits);
        } catch (Exception e) {
            throw new FailException("乘法运算出错 " + unitPrice + "*" + share);
        }
    }


    /**
     * 获取两数差
     *
     * @param minuend       被减数
     * @param reduction     减数
     * @param decimalDigits 返回字符串保留小数位数 负数不设置
     * @return
     */
    public static String subtract(String minuend, String reduction, int decimalDigits) throws FailException {
        try {
            BigDecimal totalNum = stringToNumber(minuend).subtract(stringToNumber(reduction));
            return getNumberFormat(totalNum, decimalDigits);
        } catch (Exception e) {
            throw new FailException("减法运算出错 " + minuend + "-" + reduction);
        }
    }

    /**
     * 除法运算
     *
     * @param divisor       被除数
     * @param dividend      除数 不能等于0
     * @param decimalDigits 返回字符串保留小数位数负数不设置
     * @return
     */
    public static String divide(String divisor, String dividend, int decimalDigits) throws FailException {
        try {
            BigDecimal totalNum = stringToNumber(divisor).divide(stringToNumber(dividend), decimalDigits, RoundingMode.HALF_UP);
            return getNumberFormat(totalNum, decimalDigits);
        } catch (Exception e) {
            throw new FailException("除法运算出错 " + divisor + "/" + dividend);
        }
    }

    ;

    /**
     * 除法运算[向下取整]
     *
     * @param divisor       被除数
     * @param dividend      除数 不能等于0
     * @param decimalDigits 返回字符串保留小数位数负数不设置
     * @return
     */
    public static String divideRoundDown(String divisor, String dividend, int decimalDigits) throws FailException {
        try {
            // 四舍五入  decimalDigits+10 保留的位数位数不够自动补0
            BigDecimal totalNum = stringToNumber(divisor).divide(stringToNumber(dividend), decimalDigits + 10, RoundingMode.HALF_UP);
            return getRoundDownNumberFormat(totalNum, decimalDigits);
        } catch (Exception e) {
            throw new FailException("除法运算出错 " + divisor + "/" + dividend);
        }
    }

    ;


    /**
     * 获取数字字符串的bigdecimal类型
     *
     * @param numberStr
     * @return
     */
    private static BigDecimal stringToNumber(String numberStr) {
        return new BigDecimal(numberStr);
    }

    /**
     * 获取数据转换类
     *
     * @param decimalDigits 小数位数神
     * @return
     */
    public static String getNumberFormat(BigDecimal number, int decimalDigits) {
        if (decimalDigits < 0) {
            return number.toString();
        } else if (decimalDigits == 0) {
            return "" + number.setScale(decimalDigits, BigDecimal.ROUND_HALF_UP).longValue();
        }
        String fixedStr = StringUtil.getFixedStr("#0.", decimalDigits + 3);
        DecimalFormat df = new DecimalFormat(fixedStr);
        return df.format(number.setScale(decimalDigits, BigDecimal.ROUND_HALF_UP));
    }

    /**
     * 获取数据转换类[向下取整]
     *
     * @param decimalDigits 小数位数神
     * @return
     */
    public static String getRoundDownNumberFormat(BigDecimal number, int decimalDigits) {
        if (decimalDigits < 0) {
            return number.toString();
        } else if (decimalDigits == 0) {
            return "" + number.setScale(decimalDigits, BigDecimal.ROUND_DOWN).longValue();
        }
        String fixedStr = StringUtil.getFixedStr("#0.", decimalDigits + 3);
        DecimalFormat df = new DecimalFormat(fixedStr);
        return df.format(number.setScale(decimalDigits, BigDecimal.ROUND_DOWN));
    }


    public static String formatStrToNumber(String number, int decimalDigits) {
        return getNumberFormat(stringToNumber(number), decimalDigits);
    }

    /**
     * 判断剩余额度是否大于等于买入额度
     *
     * @param buyLimit     购买额度
     * @param surplusLimit 剩余额度
     * @return
     */
    public static boolean compareBuyAndSurplusLimit(String buyLimit, String surplusLimit) {
        try {
            int compare = stringToNumber(buyLimit).compareTo(stringToNumber(surplusLimit));
            if (compare == -1 || compare == 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 加法运算
     *
     * @param numberOne     金额1
     * @param numberTwo     金额2
     * @param decimalDigits 返回字符串保留小数位数
     * @return
     */
    public static String getNumbersTotal(String numberOne, String numberTwo, int decimalDigits) throws FailException {
        try {
            BigDecimal totalNum = stringToNumber(numberOne).add(stringToNumber(numberTwo));
            return getNumberFormat(totalNum, decimalDigits);
        } catch (Exception e) {
            throw new FailException("加法运算出错 " + numberOne + "+" + numberTwo);
        }
    }

    /**
     * @param pdAmt 金额1
     * @param Idec  调整后金额小数位数
     * @return
     */
    public static Double pubDround(Double pdAmt, int Idec) {

        int iflag = 0;
        if (pdAmt < 0) {
            iflag = 1;
            pdAmt = Math.abs(pdAmt);
        }
        Double dTmpAmt = pdAmt - pdAmt.intValue();
        double v = pdAmt.intValue() + dTmpAmt + 1.0 / (Math.pow(10.0, Idec > 2 ? 15 : Idec + 13));
        if (iflag == 1) {
            return -1 * new BigDecimal(v).setScale(Idec, RoundingMode.DOWN).doubleValue();
        }
        return new BigDecimal(v).setScale(Idec, RoundingMode.DOWN).doubleValue();
    }

//		String numbersTotal = getTotal("100.1104", "5", 4);

		/*try {
			String total = getRoundDownTotal("1.23", "2.33", 2); // 2.8659

			String s1 = divideRoundDown("3.99", "0.22", 2);// 18.13636363636364
			String s2 = divideRoundDown("3.99", "0.3", 2);// 13.3   位数不够自动补零

		} catch (FailException e) {
			e.printStackTrace();
		}*/

}

